<template>
    <div>
        <van-tabs v-model="active" @change="change">
            <van-tab v-for="(item,index) in tabs" :key="index" :title="item.name">
            </van-tab>
        </van-tabs>
        <van-list class="list" v-model="loading" :finished="finished" @load="onLoad">
            <supply :manage="true" v-show="active==1" v-for="(item,index) in list" :batch="item" :key="'a'+index"></supply>
            <batch :manage="true" v-show="active==0" v-for="(item,index) in batchList" :batch="item" :key="'b'+index"></batch>
        </van-list>
        <share v-show="active==1" :batch-no="BatchNo"  :show="false"></share>
    </div>
</template>

<script>
    import share from '@/components/share'
    import api from '@/api/api'
    import supply from '@/components/supply'
    import batch from '@/components/batch'
    import {
        Toast
    } from 'vant'
    export default {
        components: {
            supply: supply,
            batch: batch,
            share:share
        },
        data() {
            return {
                BatchNo:"0",
                tabs: [{
                    name: '供应',
                    id: 1
                }, {
                    name: '需求',
                    id: 2
                }],
                allCheck: false,
                pageIndex: 1,
                pageIndex2: 1,
                loading: false,
                finished: false,
                batchList: [],
                pageSize: 25,
                active: 0,
                list: [],
                count: 0,
                running:false
            }
        },
        methods: {
            doGetList() {
                var para = {
                    OpenID: this.$store.state.userInfo.OpenID,
                    pageSize: this.pageSize,
                    pageIndex: this.pageIndex
                }
                api.getMyBatchList(para).then(res => {
                    this.batchList = res.data;
                    this.count = res.count;
                    this.isLoading = false;
                    if (this.count) {
                        Toast({
                            message: '已加载 ' + (this.count < this.pageSize ? this.count : this.pageIndex * this.pageSize) + "/" + (this.count) + ' 条记录',
                            position: 'bottom',
                            duration: 1500
                        });
                    } else {
                        Toast({
                            message: '未查询到记录',
                            position: 'bottom',
                            duration: 1500
                        });
                        this.showNoData = true;
                    }
                })
            },
            onLoad() {
                
                
                if (this.active == 0) {
                    this.doGetList();
                    return;
                }
                if(this.running)
                {
                    return;
                }
                this.running = true;
                var para = {
                    UserID: this.$store.state.userInfo.ID,
                    pageIndex: this.pageIndex,
                    pageSize: this.pageSize
                }
                api.getSupplyList(para).then(res => {
                    this.loading = false;
                    this.count = res.count;
                    this.running = false;
                    res.data.forEach(n => {
                        n.checked = false;
                        this.list.push(n);
                    });
                    if (res.data.length == 0) {
                        this.finished = true;
                        Toast({
                            message: "已加载全部",
                            position: 'bottom',
                            duration: 1500
                        })
                    } else {
                        Toast({
                            message: "已加载" + (this.pageIndex * this.pageSize > this.count ? this.count : this.pageIndex * this.pageSize) + '/' + this.count,
                            position: 'bottom',
                            duration: 1500
                        });
                        this.pageIndex++;
                    }
                })
            },
            change() {
                this.loading = false;
                this.finished = false;
                this.pageIndex = 1;
                this.list = [];
                this.onLoad();
            },
            getList() {
                this.pageIndex = 1;
                this.list = [];
                var para = {
                    pageIndex: this.pageIndex,
                    pageSize: this.pageSize,
                    OpenID: this.$store.state.userInfo.OpenID,
                    BatchType: '1,2,3,5'
                }
                api.getShopCartList(para).then(res => {
                    this.loading = false;
                    res.list.forEach(n => {
                        n.checked = false;
                    });
                    this.list = res.list;
                    this.pageIndex++;
                })
            },
            checkAll() {
                if (this.active == 0) {
                    this.list.forEach(n => {
                        n.checked = this.allCheck;
                    });
                } else {
                    this.list2.forEach(n => {
                        n.checked = this.allCheck;
                    });
                }
            },
            success(item) {
                var that = this;
                setTimeout(() => {
                    that.getList();
                }, 1000);
            },
        },
    }
</script>

<style scoped lang='less'>
.list{
    margin-bottom: 150px;
}
</style>